package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.LogToDB
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import java.text.SimpleDateFormat
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @Title: autoDocumentin
 * @Description:九级真美 短信回复定时调取任务
 * @author: Rick
 * @date 2023-03-22 10:00:00
 *
 */

public class autoDocumentin implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoDocumentin.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {


        log.debug("短信回复接口 start autoDocumentin开始-----------------");
        long startTime = System.currentTimeMillis();

        //配置信息获取
        String url = KHDBConfig.getAPIURL(eliteBean, "INSMS_URL");//接口地址"http://sms3.mobset.com";
        String CorpID = KHDBConfig.getParamConfig(eliteBean, "INSMS_CorpID");    //企业ID"301195"
        String LoginName = KHDBConfig.getParamConfig(eliteBean, "INSMS_LoginName");    //用户名"400";
        String SecretKey = KHDBConfig.getParamConfig(eliteBean, "INSMS_SecretKey");    //密码"734095";
        String HANDLEGROUP_ID = KHDBConfig.getParamConfig(eliteBean, "INSMS_HANDLEGROUPID");    //收信到哪个组";
        String HANDLEBY_ID = KHDBConfig.getParamConfig(eliteBean, "INSMS_HANDLEBYID");    //收信到哪个人";

        StringBuffer formdata = new StringBuffer();
        String resJson = "";
        String dealLog = "";
        try {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");//日期格式化
            String timeStamp = dateFormat.format(now);
            String Sign = Util.getMD5Str(CorpID + SecretKey + timeStamp);//MD5加密签名

            //参数拼接
            formdata.append("Methods=SmsGetRecv");
            formdata.append("&CorpID=" + CorpID);
            formdata.append("&LoginName=" + LoginName);
            formdata.append("&TimeStamp=" + timeStamp);
            formdata.append("&SecretKey=" + Sign);
            log.debug("短信回复接口-发送的报文信息formdata:" + formdata);

            resJson = Util.doHttpPost(url, formdata.toString(), 'UTF-8', 10000, 1);

            log.debug("短信回复接口-接口调用返回结果:" + resJson);

            //数据处理
            JSONObject jsonObj = new JSONObject(resJson);
            int code = jsonObj.getInt("Code");
            String message = jsonObj.getString("Message");
            if (code>0) {
                JSONArray Details = jsonObj.getJSONArray("Details");
                log.debug("短信回复接口-返回结果接收内容清单量:" + Details.length());
                for (int i = 0; i < Details.length(); i++) {
                    JSONObject detailObj = Details.getJSONObject(i);

                    String PhoneNumber = detailObj.getString("PhoneNumber"); //手机号码
                    String RecvNumber = detailObj.getString("RecvNumber"); //接收号码，即短信发送时的目标号码
                    String AddNum = detailObj.getString("AddNum"); //扩展号码，对应发送模块中的AddNum
                    String RecvTime = detailObj.getString("RecvTime"); //收信时间，格式如：2018-05-01 12:30:00。
                    String Content = detailObj.getString("Content"); //回复短信内容
                    if (!KHUtil.isNull(PhoneNumber)) {
                        eliteBean.update("insert into documentin(DOCUMENTIN_GUID,DOCTYPE,status,documentpath,documentfrom,documentto,RECEIVEDDATETIME,HANDLEBY_ID,HANDLEGROUP_ID,MODIFIEDDATE,STRINGFIELD1)values(newid(),3,0,'" + Content + "','" + PhoneNumber + "','" + RecvNumber + "','" + RecvTime + "','" + HANDLEBY_ID + "','" + HANDLEGROUP_ID + "',getdate(),'" + AddNum + "')", "dataSource");
                        //检查是否存在

                        //满意度数据更新--只匹配手机号码存在、收信时间和发信时间为同一天、第一次更新的数据
                        if (Content.equals("1") || Content.equals("2") || Content.equals("3") || Content.equals("4")) {
                            eliteBean.update("update jm_satisfaction_list set reply_time='" + RecvTime + "',reply_content='" + Content + "',satisfaction_value='" + Content + "' where send_no='" + PhoneNumber + "' and CONVERT(varchar(10),send_time,23)=CONVERT(varchar(10),'" + RecvTime + "',23) and reply_content is null", "dataSource");
                        }
                    }
                }
                log.debug("短信回复接口-数据处理完成...");
            } else {
                log.debug("短信回复接口-返回无结果-code:" + code + " message：" + message);
            }

        } catch (Exception e) {
            dealLog = "短信回复接口-调用失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (System.currentTimeMillis() - startTime);
            log.error(dealLog, e);
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "短信回复接口调用", "autoDocumentin", formdata.toString(), resJson, String.valueOf((System.currentTimeMillis() - startTime)), "调用URL:" + url + System.lineSeparator() + dealLog);
        }
    }
}